package com.mercury.game.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mercury.game.entity.Tree;
import com.mercury.game.service.TreeService;
import com.mercury.user.service.CenterUserService;
import com.mercury.util.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 童雅婷
 * @since 2021-11-23
 */
@RestController
@RequestMapping("/tree")
@Api(value = "游戏种树模块")
public class TreeController {

    @Autowired
    private TreeService service;

    @Autowired
    private CenterUserService userService;

    /**
     * @param tree
     * @return
     * @date 2021/11/23 8:58
     * @description 用户创建树 ，
     */
    @PostMapping("/tree")
    @ApiOperation("用户创建树")
    public R createTree(@RequestBody Tree tree) {

        boolean save = service.createTree(tree);
        if (save) return R.ok().message("创建树成功");
        else return R.ok().message("创建树失败");
    }


    @GetMapping("/tree/{userId}")
    @ApiOperation("进入界面，获得用户对应的那棵树")
    public R getTree(@PathVariable String userId) {
        QueryWrapper<Tree> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        Tree tree = service.getOne(wrapper);
        return R.ok().data("tree", tree);
    }

    /**
     * @param id
     * @param type
     * @return
     * @date 2021/11/23 19:49
     * @description 用户完成任务获取水滴
     */
    @GetMapping("/water/{id}/{type}")
    @ApiOperation("用户根据不同的任务获得水滴")
    public R getWater(@PathVariable String id,
                      @PathVariable String type) {

        Tree tree = service.updateWater(id, type);
        return R.ok().data("tree",tree);

    }


    /**
     * @return
     * @date 2021/11/23 19:49
     * @params null
     * @description 用户浇水，如果水量到达一定的量，树会升级和获得咖啡豆
     */
    @GetMapping("/water/{id}")
    @ApiOperation("用户浇水消耗水量")
    public R useWater(@PathVariable int id) {
        Tree tree = service.getById(id);
        if(tree.getUserWater() < 10) return R.error().message("水量不足");
        tree = service.useWater(id);
        int beanNum = tree.getTreeWater() > 90 ? service.setUserBeans(id) : 0;
        if(beanNum > 0) return R.ok().data("tree", tree).message(beanNum + "");
        else return R.ok().data("tree", tree);
    }


}

